Business Rule and Script Include Example

Script Include and Business Rule Example:

Business Rule: (After Insert)

(function executeRule(current, previous /*null when async*/) {
var createP=new ProblemUtils();
var problemSysid=createP.createProblemRecord(current); //Create a Problem

var getProblemRecrod=createP.getRecrodNumber(problemSysid,"problem");  //get Problem Number

createP.updateParent(getProblemRecrod,"problem",current); // Update problem with Parent field

gs.addInfoMessage(getProblemRecrod);
})(current, previous);

 

 

Script Include:(Not Client Callable)

var ProblemUtils = Class.create();
ProblemUtils.prototype = {
initialize: function() {
},

createProblemRecord:function(current){

var createProblem=new GlideRecord("problem"); // GlideRecord API is used to Query the table, Table name=problem

createProblem.initialize(); // initialization of object

createProblem.short_description=current.short_description + "Assingedto:" +current.assigned_to.name; //mapping

createProblem.assigned_to=current.assigned_to; ////mapping

createProblem.assignment_group=current.assignment_group; //mapping

createProblem.description=current.description ; //mapping

var problemSysid=createProblem.insert();

return problemSysid;
},

getRecrodNumber:function (sysid,tableName){

var getNumber=new GlideRecord(""+tableName);
getNumber.addQuery("sys_id",sysid);
getNumber.query();
if(getNumber.next()){
return getNumber.number;
}else{

return "No Record found";
}

},

updateParent:function(number,tableName,current){

var getNumber=new GlideRecord(""+tableName);
getNumber.addQuery("number",number);
getNumber.query();
if(getNumber.next()){
getNumber.parent=current.sys_id;
getNumber.description=current.number;
getNumber.update();
}

},

type: 'ProblemUtils'
};